package cn.jd.system.service.impl;

import cn.jd.basic.util.PageList;
import cn.jd.system.domain.Menu;
import cn.jd.system.service.IMenuService;
import cn.jd.basic.service.impl.BasicServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jd
 * @since 2022-10-24
 */
@Service
public class MenuServiceImpl extends BasicServiceImpl<Menu> implements IMenuService {

    @Override
    public List<Menu> first() {
        List<Menu> menuList = super.findAll();
        List<Menu> list = menuList.stream().filter(men -> null == men.getParentId()).collect(Collectors.toList());
        return list;
    }

    /**
     * 菜单无线级数
     * @return
     */
    @Override
    public List<Menu> findMenuree() {
        List<Menu> list = new ArrayList<>();
        List<Menu> menuList = super.findAll();
        Map<Long, Menu> collect = menuList.stream().collect(Collectors.toMap(Menu::getId, e -> e));
        menuList.forEach(menu -> {
            if(menu.getParentId()==null){
                list.add(menu);
            }else {
                Menu parent = collect.get(menu.getParentId());
                if(parent==null){
                    return;
                }
               parent.getChildren().add(menu);

            }
        });
        return list;
    }
}
